﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Silverlight.Reporting.TestClient.Services;
using System.Windows.Printing;

namespace Silverlight.Reporting.TestClient
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            InitializeReport();
        }


        private ReportTotals _totals;

        private void InitializeReport()
        {
            Report.EndPrint += (s, e) =>
                {
                    //MessageBox.Show("Report printed.");
                    StatusText.Text = "Report printed.";
                };

            Report.BeginBuildReport += (s, e) =>
                {
                    StatusText.Text = "Building and printing report.";
                    // initialize running totals
                    _totals = new ReportTotals();
                };

            Report.BeginBuildReportItem += (s, e) =>
                {
                    // this event fires with each report item, so you
                    // can use it to build a running total
                    var item = e.DataContext as EmployeeReviewReportItem;

                    _totals.EmployeeCount++;
                    _totals.TotalBonus += item.Bonus;
                    _totals.TotalSalary += item.Salary;
                };

            Report.BeginBuildReportFooter += (s, e) =>
                {
                    // set the running total as the context for the report footer
                    e.DataContext = _totals;
                };

        }

        private void LoadData_Click(object sender, RoutedEventArgs e)
        {
            var client = new DataServiceClient();

            client.GetEmployeeReviewReportDataCompleted += (s, ea) =>
                {
                    Report.ItemsSource = ea.Result;

                    if (ea.Result.Count > 0)
                    {
                        PrintReport.IsEnabled = true;
                        PrintReportForceVector.IsEnabled = true;
                        StatusText.Text = "Data Loaded. Report can now be printed.";
                    }
                    else
                    {
                        PrintReport.IsEnabled = false;
                        PrintReportForceVector.IsEnabled = false;
                        StatusText.Text = "Failed to load data.";
                    }
                };

            client.GetEmployeeReviewReportDataAsync();
        }

        private void PrintReport_Click(object sender, RoutedEventArgs e)
        {
            Report.Print();
        }

        private void PrintReportForceVector_Click(object sender, RoutedEventArgs e)
        {
            // keep in mind that forcing vector output will ONLY work if you have a postscript driver 
            // for your printer *and* that's the driver you select to print to.

            var settings = new PrinterFallbackSettings();
            settings.ForceVector = true;

            Report.Print(settings);
        }
    }
}
